<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">下载器名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="d_name" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">连接IP</label>
                        <div class="layui-input-block">
                            <input type="text" name="d_host" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">访问端口</label>
                        <div class="layui-input-block">
                            <input type="text" name="w_port" value="8987" required lay-verify="required" class="layui-input"><span style="color: #cccccc">工具端口默认8987</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">下载器端口</label>
                        <div class="layui-input-block">
                            <input type="text" name="d_port" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">下载器用户名</label>
                        <div class="layui-input-block">
                            <input type="text" name="d_username" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">下载器密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="d_password" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">上传限速(MB/s)</label>
                        <div class="layui-input-block">
                            <input type="text" name="up_speed" value="" required lay-verify="required" class="layui-input" ><span style="color: #cccccc">最大上传限速，留空不限速</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">下载限速(MB/s)</label>
                        <div class="layui-input-block">
                            <input type="text" name="down_speed" value="" required lay-verify="required" class="layui-input"  ><span style="color: #cccccc">设置最大下载限速，留空不限速</span>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">同时下载(个)</label>
                        <div class="layui-input-block">
                            <input type="text" name="task_limit" value="" required lay-verify="required" class="layui-input" ><span style="color: #cccccc">请配置最大同时任务数量，留空默认0为不下载</span>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">最大硬盘(GB)</label>
                        <div class="layui-input-block">
                            <input type="text" name="task_size" value="" required lay-verify="required" class="layui-input" ><span style="color: #cccccc">配置最大可用的硬盘大小(GB)，留空默认0为不下载</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">任务体积下限(GB)</label>
                        <div class="layui-input-block">
                            <input type="text" name="task_torrent_min_size" value="0" class="layui-input"><span style="color: #cccccc">留空或0表示不限制</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">任务体积上限(GB)</label>
                        <div class="layui-input-block">
                            <input type="text" name="task_torrent_max_size" value="0" class="layui-input"><span style="color: #cccccc">留空或0表示不限制</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">备注</label>
                        <div class="layui-input-block">
                            <input type="text" name="note" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">启用</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="status" lay-filter="status" lay-skin="switch" />
                            <input type="text" style="display:none" name="status" value="0" />
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/csauto/downloader/select" + location.search;
            const UPDATE_API = "/app/csauto/downloader/update";

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(layui.util.escape(value));
                            } else {
                                obj.attr("value", value);
                            }
                        });


                        

                        
                        // 字段 禁用 status
                        layui.use(["form"], function() {
                            layui.$("#status").attr("checked", layui.$('input[name="status"]').val() != 0);
                            layui.form.render();
                            layui.form.on("switch(status)", function(data) {
                                layui.$('input[name="status"]').val(this.checked ? 1 : 0);
                            });
                        })

                        // ajax产生错误
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
